<?php

import("apps.AbstractApp");

class ImageStreamApp extends AbstarctApp {
	
	var $CT_LIST	= "list";
	var $CT_SHOW 	= "show";
	
	var $form;
	var $pageLocation;
	var $errors = array();
	var $resultProccessRequest = false;
	var $receiver = "igor.s.melnik@gmail.com";
	var $repository;
	var $listOfImages = array();
	var $currImage = null;
	var $useTemplate = null;
	
	var $defCatId = "imgs"; // catalogue id for this application
	var $content_type = "image";
	
	function ImageStreamApp() {
		import("net.html.form.Form");
		$this->form = new Form("feedbackForm");
		
		import("data.logic.file.ImageRepository");
		$this->repository = new ImageRepository(REPOSITORY_PATH);
		
		//$fileContent = $repository->getContent("1314078493");
	}
	
	function getForm() {
		return $this->form;
	}
	
	function proccessRequest($request) {
		if (!$request) return;

		$rp = $this->getAdditionalParams();

		if (isset($rp[2]) && (strlen($rp[2])==0 || $rp[2]==$this->defCatId.":root")) unset($rp[2]);
		
		if (isset($rp[2]) && isset($rp[3])) {
			// category && image
			$this->listOfImages = $this->repository->findAllByCategory($rp[2]);
print "-".$rp[2];
print_r($this->listOfImages);
			foreach ($this->listOfImages as $imageInfo) {
				if ($imageInfo->getId()==$rp[3]) {
					$this->currImage = $imageInfo;
				}
			}
			$this->content_type = $this->CT_SHOW;
			
		 } elseif (isset($rp[2])) {
			// category
			$this->listOfImages = $this->repository->findAllByCategory($rp[2]);
print "-".$rp[2];
print_r($this->listOfImages);
			if (count($this->listOfImages)>0)
				$this->currImage = $this->listOfImages[0];
			$this->content_type = $this->CT_LIST;
			
		} else {
			if (isset($rp[1]) && $rp[1]=="show")
				header("Location: /");
		}
	}
	
	function hasErrors() {
		return count($this->errors);
	}
	
	function addError($key, $message) {
		$this->errors[$key] = $message;
	}
	
	function getErrors() {
		return $this->errors;
	}
	
	function assignToTemplates(&$templates) {
		//Smarty
		$templates->assign("img_list", $this->listOfImages);
		$templates->assign("img_current", $this->currImage);
		$templates->assign_by_ref("controller", $this);
		//$templates->assign("title_page", $this->page_title);
		$templates->assign("content_type", $this->content_type);
		
		$templates->assign("forms",array($this->form->getName()=>$this->form->toArray()) );
		if ($this->resultProccessRequest) {
			$templates->assign("result",array($this->form->getName()=>array("message"=>"Your message has been sent")) );
		}
	}
}
?>